/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: ListAssignedInstrumentsPresenter.java
 * Date: 04/02/2011
 */

package com.ingenius.sau.client.presenter;

import java.util.List;

import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.EditAssignedInstrumentEvent;
import com.ingenius.sau.client.service.CoreServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.instruments.AssignedInstrument;
import com.ingenius.sau.shared.model.programs.AcademicProgram;
import com.ingenius.sau.shared.util.filter.Filter;

/**
 * Class <strong>ListAssignedInstrumentsPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class ListAssignedInstrumentsPresenter implements
		ListPresenter<AssignedInstrument> {

	public interface ListAssignedInstrumentsDisplay extends Display {
		void reload();
	}

	private final HandlerManager eventBus;
	private final CoreServiceAsync rpcService;
	private final ListAssignedInstrumentsDisplay display;
	private String selectedAssignedInstrumentId;

	/**
	 * 
	 * @param eventBus
	 * @param rpcService
	 * @param display
	 */
	public ListAssignedInstrumentsPresenter(HandlerManager eventBus,
			CoreServiceAsync rpcService, ListAssignedInstrumentsDisplay display) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;

		selectedAssignedInstrumentId = null;
		display.setPresenter(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doAdd()
	 */
	@Override
	public void doAdd() {
		fireEvent(new EditAssignedInstrumentEvent(null));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doEdit()
	 */
	@Override
	public void doEdit() {
		fireEvent(new EditAssignedInstrumentEvent(selectedAssignedInstrumentId));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#doDelete()
	 */
	@Override
	public void doDelete() {
		// TODO i18n
		final String failedMessage = "Failed to delete assigned instrument";
		final String successMessage = "Assigned instrument deleted sucessfully";

		AppController.setStatusMessage("Deleting...");
		rpcService.deleteAssignedInstrument(selectedAssignedInstrumentId,
				new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if (result.booleanValue() == true) {
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_DELETE
								+ AcademicProgram.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
						display.reload();
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.mvp.presenters.ListPresenter#goInto()
	 */
	@Override
	public void goInto() {
		return;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.presenter.ListPresenter#fireEvent()
	 */
	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#setSelectedItemId
	 * (java.lang.String)
	 */
	@Override
	public void setSelectedItemId(String id) {
		this.selectedAssignedInstrumentId = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.Presenter#go(com.google.gwt.user
	 * .client.ui.HasWidgets)
	 */
	@Override
	public void go(HasWidgets container) {
		container.clear();
		container.add(display.asWidget());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ingenius.sau.client.mvp.presenters.ListPresenter#loadData(java.lang
	 * .List, java.lang.Object, com.google.gwt.user.client.rpc.AsyncCallback)
	 */
	@Override
	public void loadData(List<Filter> filters, Object config,
			AsyncCallback<PagingLoadResult<AssignedInstrument>> callback) {

		rpcService.findAssignedInstruments(filters,
				(BasePagingLoadConfig) config, callback);

	}

	@Override
	public String getModuleName() {
		return Constants.TOKEN_PROCESSES;
	}

}
